package org.example.datasoure.dataSource;

import java.sql.*;
import java.util.Properties;

/**
 * @ClassName: DbUtil
 * @Description:
 * @Author: YourName
 * @Date: 2025/1/8
 */

public class DbUtil {

    // 方式1
    // 方式2
    public static void connect02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        // 使用反射加载 Driver类 动态加载 更加灵活 减少依赖性
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();
        String url = "jdbc:mysql://localhost:3306/wiglapp?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai";
        // 将用户名和密码封装到properties对象中
        Properties properties = new Properties();
        // 说明user和password是规定好的 后面的值根据实际情况写
        properties.setProperty("user", "root");
        properties.setProperty("password", "root");
        Connection connect = driver.connect(url, properties);
        String sql = "select * from T_STUDENT";
        Statement statement = connect.createStatement();
        ResultSet res = statement.executeQuery(sql);
        System.out.println(res.toString());
        // 关闭连接
        statement.close();
        connect.close();
    }
}
